


package com.starmark.gateway.project.provider.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starmark.auth.common.util.UserUtil;
import com.starmark.core.framework.base.AbstractController;
import com.starmark.core.framework.response.SuccessResponseData;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.core.framework.vo.PageQueryVo;
import com.starmark.gateway.project.provider.entity.GatewayProjectSpecUrl;
import com.starmark.gateway.project.provider.service.IGatewayProjectService;
import com.starmark.gateway.project.provider.service.IGatewayProjectSpecUrlService;
import com.starmark.gateway.project.provider.vo.GatewayProjectSpecUrlPageVo;
import com.starmark.gateway.project.provider.vo.GatewayProjectSpecUrlVo;
import com.starmark.gateway.project.provider.vo.GatewayProjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 工程特定网址管理 前端控制器
 * </p>
 *
 * @author starmark
 * @since 2020-09-29
 */
@RestController
@Api(value = " 工程特定网址管理管理", description = " 工程特定网址管理管理")
@RequestMapping("gateway/project/spec/url")
public class GatewayProjectSpecUrlController extends AbstractController {
    @Autowired
    IGatewayProjectSpecUrlService gatewayProjectSpecUrlService;

    @Autowired
    private IGatewayProjectService gatewayProjectService;


    @ApiOperation(value = "查询工程特定网址管理列表")
    @PostMapping(value = "/query")
    public Page<GatewayProjectSpecUrlPageVo> query(@RequestBody PageQueryVo pageQueryVo) {
        GatewayProjectVo projectVo=gatewayProjectService.getGatewayAuthProjectVoByProjectCode(UserUtil.getCurrentUser().getProjectCode());
        Assert.notNull(projectVo,"工程不存在:"+UserUtil.getCurrentUser().getProjectCode());
        pageQueryVo.getParams().put("project_id",projectVo.getId());
        return BeanMapper.mapPage(gatewayProjectSpecUrlService.queryPage(pageQueryVo), GatewayProjectSpecUrlPageVo.class);
    }


    @ApiOperation(value = "新增工程特定网址管理")
    @PostMapping
    public SuccessResponseData add(@RequestBody GatewayProjectSpecUrlVo gatewayProjectSpecUrlVo) {
        GatewayProjectVo projectVo=gatewayProjectService.getGatewayAuthProjectVoByProjectCode(UserUtil.getCurrentUser().getProjectCode());
        Assert.notNull(projectVo,"工程不存在:"+UserUtil.getCurrentUser().getProjectCode());
        GatewayProjectSpecUrl gatewayProjectSpecUrl = BeanMapper.map(gatewayProjectSpecUrlVo, GatewayProjectSpecUrl.class);
        gatewayProjectSpecUrl.setProjectId(projectVo.getId());
        gatewayProjectSpecUrlService.save(gatewayProjectSpecUrl);
        return SuccessResponseData.newInstance(gatewayProjectSpecUrl.getId());
    }

    @ApiOperation(value = "工程特定网址管理详情")
    @GetMapping(value = "/{id}")
    public GatewayProjectSpecUrlVo get(@PathVariable("id") String id) {
        GatewayProjectSpecUrl gatewayProjectSpecUrl = gatewayProjectSpecUrlService.getById(id);
        return BeanMapper.map(gatewayProjectSpecUrl, GatewayProjectSpecUrlVo.class);
    }

    @PutMapping
    @ApiOperation(value = "修改工程特定网址管理")
    public SuccessResponseData update(@RequestBody GatewayProjectSpecUrlVo gatewayProjectSpecUrlVo) {
        GatewayProjectVo projectVo=gatewayProjectService.getGatewayAuthProjectVoByProjectCode(UserUtil.getCurrentUser().getProjectCode());
        Assert.notNull(projectVo,"工程不存在:"+UserUtil.getCurrentUser().getProjectCode());
        GatewayProjectSpecUrl gatewayProjectSpecUrl = BeanMapper.map(gatewayProjectSpecUrlVo, GatewayProjectSpecUrl.class);
        gatewayProjectSpecUrl.setProjectId(projectVo.getId());
        gatewayProjectSpecUrlService.updateById(gatewayProjectSpecUrl);
        return SuccessResponseData.newInstance(gatewayProjectSpecUrl.getId());
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除 工程特定网址管理")
    public void delete(@PathVariable("id") String id) {

        gatewayProjectSpecUrlService.removeById(id);
    }

}
